package gui;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import persistencia.ConsultasExternasDAO;
import persistencia.ProcesoasistencialDAO;
import pojos.Consultas;
import pojos.Procesoasistencial;

/**/


public class JDialogConsultasExternas extends javax.swing.JDialog {

    PanelConsultasExternas panel;
    int proceso;
    ConsultasExternasDAO dao=new ConsultasExternasDAO();
    
    public JDialogConsultasExternas(int proceso,PanelConsultasExternas panel) {
        this.proceso=proceso;
        this.panel=panel;
        initComponents();
        switch(proceso){
                case 1:
                    fieldIdConsulta.setVisible(false);
                    labelIdConsulta.setVisible(false);                    
                    fieldFecha.setText(new Date().getDate()+"-"+(new Date().getMonth()+1)+"-"+(new Date().getYear()+1900));
                    fieldHoraEntrada.setText(new Date().getHours()+":"+new Date().getMinutes());
                    fielHoraSalida.setEnabled(false);
                    break;                
                case 2:
                    labelInstrucciones.setText("Introduzca la id de la consulta que desee eliminar");
                    btnGuardar.setText("Borrar");
                    fieldEspecialidad.setVisible(false);
                    fieldFecha.setVisible(false);
                    fieldHoraEntrada.setVisible(false);
                    fielHoraSalida.setVisible(false);
                    comboProcAsist.setVisible(false);
                    labelEspecialidad.setVisible(false);
                    labelFecha.setVisible(false);
                    labelHoraEntrada.setVisible(false);
                    labelHoraSalida.setVisible(false);
                    labelProcAsist.setVisible(false);
                    break;
                case 3:
                    labelInstrucciones.setText("Introduzca la id de la consulta que desee modificar");
                    btnGuardar.setText("Modificar");
                    fieldFecha.setText(new Date().getDate()+"-"+(new Date().getMonth()+1)+"-"+(new Date().getYear()+1900));
                    fieldHoraEntrada.setText(new Date().getHours()+":"+new Date().getMinutes());
                    fielHoraSalida.setText(new Date().getHours()+":"+new Date().getMinutes());
                    break;
            }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnCancelar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        labelIdConsulta = new javax.swing.JLabel();
        labelFecha = new javax.swing.JLabel();
        labelHoraEntrada = new javax.swing.JLabel();
        labelHoraSalida = new javax.swing.JLabel();
        labelEspecialidad = new javax.swing.JLabel();
        labelProcAsist = new javax.swing.JLabel();
        fieldIdConsulta = new javax.swing.JTextField();
        comboProcAsist = new javax.swing.JComboBox();
        fieldHoraEntrada = new javax.swing.JTextField();
        fielHoraSalida = new javax.swing.JTextField();
        fieldEspecialidad = new javax.swing.JTextField();
        fieldFecha = new javax.swing.JTextField();
        labelInstrucciones = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnCancelar.setText("cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnGuardar.setText("guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        labelIdConsulta.setText("idConsulta");

        labelFecha.setText("fecha");

        labelHoraEntrada.setText("hora entrada");

        labelHoraSalida.setText("hora salida");

        labelEspecialidad.setText("especialidad");

        labelProcAsist.setText("idProceso asistencial");

        comboProcAsist.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(labelInstrucciones)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnCancelar)
                                .addGap(104, 104, 104)
                                .addComponent(btnGuardar))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(labelProcAsist)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(comboProcAsist, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(labelFecha)
                                        .addComponent(labelIdConsulta)
                                        .addComponent(labelHoraEntrada)
                                        .addComponent(labelHoraSalida)
                                        .addComponent(labelEspecialidad))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(fieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(fieldIdConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(fieldHoraEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(fielHoraSalida, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(fieldEspecialidad, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(40, 40, 40))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(labelInstrucciones)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelIdConsulta)
                    .addComponent(fieldIdConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelFecha)
                    .addComponent(fieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelHoraEntrada)
                    .addComponent(fieldHoraEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelHoraSalida)
                    .addComponent(fielHoraSalida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEspecialidad)
                    .addComponent(fieldEspecialidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelProcAsist)
                    .addComponent(comboProcAsist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnGuardar))
                .addGap(40, 40, 40))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        try {
            this.setVisible(false);
            switch(proceso){
                case 1:agregar();break;
                case 2:eliminar();break;
                case 3:modificar();break;
            }
        } catch (ParseException ex) {
            Logger.getLogger(JDialogConsultasExternas.class.getName()).log(Level.SEVERE, null, ex);
        }
        panel.Buscar();
    }//GEN-LAST:event_btnGuardarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox comboProcAsist;
    private javax.swing.JTextField fielHoraSalida;
    private javax.swing.JTextField fieldEspecialidad;
    private javax.swing.JTextField fieldFecha;
    private javax.swing.JTextField fieldHoraEntrada;
    private javax.swing.JTextField fieldIdConsulta;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelEspecialidad;
    private javax.swing.JLabel labelFecha;
    private javax.swing.JLabel labelHoraEntrada;
    private javax.swing.JLabel labelHoraSalida;
    private javax.swing.JLabel labelIdConsulta;
    private javax.swing.JLabel labelInstrucciones;
    private javax.swing.JLabel labelProcAsist;
    // End of variables declaration//GEN-END:variables

    private void agregar() throws ParseException {   
        Consultas agregar=new Consultas();
        agregar.setFecha(new Date());
        agregar.setHoraEntrada(new Date());
        agregar.setEspecialidad(fieldEspecialidad.getText());
        agregar.setProcesoasistencial(new ProcesoasistencialDAO().obtenProcesoasistencial(1));
        dao.guardaConsultaExterna(agregar);
    }

    private void eliminar() {
        int idAEliminar=Integer.parseInt(fieldIdConsulta.getText());        
        Consultas personaBuscada = dao.obtenConsultas(idAEliminar);
        dao.eliminaConsultas(personaBuscada);
    }

    private void modificar() {
        int idAModificar=Integer.parseInt(fieldIdConsulta.getText());
        Consultas personaBuscada = dao.obtenConsultas(idAModificar);
        personaBuscada.setFecha(new Date());
        personaBuscada.setHoraEntrada(new Date());
        personaBuscada.setHoraSalida(new Date());
        personaBuscada.setEspecialidad(fieldEspecialidad.getText());
        dao.actualizaConsultas(personaBuscada);
    }
}
